#!/bin/bash
#set -n
set -x
start_secs=$SECONDS
export ii=$II
export i=$I
dir=$1
if [[ -z "$dir" ]] ; then
  echo "Must give dir" >&2
  exit 1
fi
lsvn_loc=$2
export lsvn_parm=
if [[ -n "$lsvn_loc" ]] ; then
	export SVN_LOC=$lsvn_loc
	export lsvn_parm="-l $lsvn_loc"
elif [[ -z "$SVN_LOC" ]] ; then
	export SVN_LOC=$CVS_LOC
fi
set +x
cd $dir
if (( $? != 0 )) ; then
  echo "Cannot cd to $ii/$dir" >&2
  exit 1
fi
function mk_svn_fls {
echo ":!rm \$II/whyaddl" > $ii/whyadd
echo ":e! \$II/whyaddl" >> $ii/whyadd
/bin/grep '^?' $tmpa | grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' -e 'tmp$' | \
    /bin/sed -e "s#^. #:!\$I/addsvn.sh $lsvn_parm $dir '#g" -e "s/  / /g" \
	-e "s/\$/' /g" | sort -u >> $ii/whyadd
echo ":!rm \$II/whyupdl" > $ii/whyupd
echo ":e! \$II/whyupdl" >> $ii/whyupd
/bin/grep '^[MGCU]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' | \
    /bin/sed -e "s#^. #:!\$I/updatesvn.sh $lsvn_parm $dir '#g" -e "s/  / /g" \
	-e "s/\$/' /g" | sort -u >> $ii/whyupd
/bin/grep '^[D]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' | \
    /bin/sed -e "s#^. ##g" > $ii/tmpwhydelfn
/bin/grep '^[!]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' | \
    sed -e 's/! *//g' > $ii/tmpwhydelfnin
cat $ii/tmpwhydel | \
while read fn ; do
	$i/checkfile.sh "$fn" $ii/tmpwhydelfn
done
echo ":!rm \$II/whydell" > $ii/whycmt
echo "\":e! \$II/whydell" >> $ii/whycmt
/bin/sed -e "s#^#:!\$I/delsvn.sh $lsvn_parm $dir '#g" -e "s/\$/' /g" \
	< $ii/tmpwhydelfn > $ii/whydel
/bin/grep '^[C]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' | \
    /bin/sed -e "s#^. #:!\$I/resolvedsvn.sh $dir '#g"  -e "s/  / /g" \
	-e "s/\$/' /g" | sort -u > $ii/whyres
echo ":!rm \$II/whycmtl" > $ii/whycmt
echo "\":e! \$II/whycmtl" >> $ii/whycmt
/bin/grep '^[?ADCGM]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' -e 'tmp$' | \
    /bin/sed -e "s#^. *#:!\$I/commitsvn.sh $lsvn_parm $dir '#g" -e "s/  / /g" \
	-e "s/\$/' /g" | \
	sort -u >> $ii/whycmt
echo ":!rm \$II/whyresl" > $ii/whyres
/bin/grep '^[?ADCGM]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' -e 'target$' | \
    /bin/sed -e "s#^. *#:!\$I/resolvesvn.sh $lsvn_parm $dir '#g" -e "s/  / /g" \
	-e "s/\$/' /g" | \
	sort -u >> $ii/whyres
echo "\":e! whysvn.diff" > $ii/whydiff
echo ":!rm -f whysvn.diff" >> $ii/whydiff
/bin/grep '^[CGM]' $tmpa |  grep -v -e 'zipedsrc/' -e 'swp$' | \
    /bin/sed -e "s#^. *#:!\$I/diffsvn.sh $dir '#g" -e "s/  / /g" \
	-e "s/\$/' /g" | sort -u >> $ii/whydiff
end_secs=$SECONDS
echo \
  "ELAPSED=$(( (end_secs - start_secs)/60 )):$(( (end_secs - start_secs)%60 ))"
}
sprojdir="$ii/svnprojs/$(basename $dir)"
mkdir -p $sprojdir
if (( $? != 0 )) ; then
	echo "Error cannot make dir $sprojdir"
	exit 1
fi
if [[ (! -f $sprojdir/cpionbr) || ( ! -s $sprojdir/cpionbr) ]] ; then
	echo 1 > $sprojdir/cpionbr
	if (( $? != 0 )) ; then
		echo "Error cannot create $sprojdir/cpionbr"
		exit 1
	fi
fi
ocpio_nbr=$(<$sprojdir/cpionbr)
(( cpio_nbr = ocpio_nbr + 1 ))
echo $cpio_nbr > $sprojdir/cpionbr
cpiofle="$sprojdir/src$cpio_nbr.cpio.gz"
sh -c "find . -type d | grep -v -e ./bin -e ./res -e svn -e ./nbp ;\
    find . -type f | grep -v svn" | cpio -oc | \
	gzip -c > $cpiofle
if (( $? != 0 )) ; then
	echo "cpio failed for $cpiofle"
	exit 1
fi
ls -l $cpiofle
if [[ ! -s $cpiofle ]] ; then
	echo "cpio failed for $cpiofle"
	exit 1
fi
#test tmpa=$ii/tmpa
tmpa=$ii/whyall
printf "Use update?";read use_update
if [[ "$use_update" = "y" ]] ; then
	printf "Are you sure that you want to use update?";read use_update
	if [[ "$use_update" = "y" ]] ; then
		echo '$SVN_LOC/svn update . > $tmpa 2>&1'
		mk_svn_fls
		exit
	fi
fi
$SVN_LOC/svn --no-ignore status . > $tmpa 2>&1
if (( $? != 0 )) ; then
  echo "Cannot svn $ii/$dir" >&2
  grep -q ' src' $tmpa 2>&1
  if (( $? == 0 )) ; then
    echo "Get src from svn for $ii/$dir continuing"
  else
    echo "Cannot svn $ii/$dir" >&2
		more $tmpa
    exit 1
  fi
fi
mk_svn_fls
